function [nrec] = analysis2unv

%  Created 8/21/03 by B. Ross
%  This function will create a universal file containing the records
%  of the last analysis in the Project structure for all the channels
%  in the Project structure.  This function uses the SAVEN routines
%  to create the universal file.
%
%  On 8/27/03 changed the call to uniwrite function to a modified function
%  that handles unevenly spaced data as well as evenly spaced data.
%
%  On 9/9/03 modified the UNV filename prompting.  The changes allow the
%  user to specify both a filename and path.  Also, if the file already
%  exists, the user will be prompted to either append or overwrite that 
%  file.  **NOTE**: If the user wishes to append a file, they must answer YES
%  to the OVERWRITE prompt the system generates when they select the file.
%  This function will then generate another prompt asking whether to append
%  or overwrite.  This is an issue with using the UIPUTFILE function.
%
%  On 1/11/05 cleaned up some comments and changed function name to
%  ANALYSIS2UNV.
%
%  Last modified on 1/11/05 by B. Ross
%
%  ************************************************************************
%  [num_records_written] = analysis2unv
%
%  num_records_written = number of records(channels) written to UNV file
%  *************************************************************************

global Project;

nrec = 0;

%  Check for existance of analysis data in current Project

content = fieldnames(Project.Test_Item.Run.Channel(1));
if strcmp(content{size(content,1)},'Analysis')

	%  Prompt for Analysis Number to Export
	
	[anal_num, varargout] = analysis_prompt;
	
	%  Select Channels to Export
	
	[fchan, lchan, varargout] = channel_select;
	
    %  Prompt for UNV filename
	
	[fname,pname] = uiputfile([Project.Test_Item.Run.Setup_File '.unv'],...
                        'Select Export Universal File');
	if isequal([fname, pname], [0,0])
	    return
	end
	%  If file exists, prompt for Append or Overwrite
	filename = fullfile(pname, fname);
    if exist(filename) == 2
        [status] = unvquery;
    else
        status = 1;
    end 
    
	%  Create default header
	
	head = univhead('default');
	
	%  Replace default header fields with our specific data
	
	%  Common to all channels in analysis
	xdelta = diff(Project.Test_Item.Run.Channel(1).Analysis(anal_num).Abs);
	switch Project.Test_Item.Run.Channel(1).Analysis(anal_num).Type
	case 1 %Time History
        functype = 1;
        absspc = 1;
	case 2 %PSD
        functype = 9;
        absspc = 1;
	case 3 %FRF
        functype = 4;
        absspc = 1;
	case 4 %Sine Mag vs. Freq
        functype = 12;
        absspc = 0;
	case 5 %Sine FRF
        functype = 4;
        absspc = 0;
	case 6 %SRS
        functype = 12;
        absspc = 1;
	case 7 %Third Octave
        functype = 9; % NOT SURE ABOUT THIS ASSIGNMENT
        absspc = 1;
	end
	
	if Project.Test_Item.Run.Channel(1).Analysis(anal_num).Type == 1
        xtype = 17; %Time
	else
        xtype = 18; %Frequency
	end
	head = univhead('xmin',Project.Test_Item.Run.Channel(1).Analysis(anal_num).Abs(1),head);
	head = univhead('xdelta',xdelta(1),head);
	head = univhead('functype',functype,head);
	head = univhead('xtype',xtype,head);
	head = univhead('yntype',12,head);
	
	%  For each channel selected ...
	respdir = 0;
	for i=fchan:lchan
        switch Project.Test_Item.Run.Channel(i).Direction
			case 'X'
                respdir = 1;
			case 'x'
                respdir = 1;
			case 'Y'
                respdir = 2;    
			case 'y'
                respdir = 2;
			case 'Z'
                respdir = 3;
			case 'z'
                respdir = 3;
        end
        if isempty(str2num(Project.Test_Item.Run.Channel(i).Coordinate))
            Project.Test_Item.Run.Channel(i).Coordinate = '9999';
        end
        if isempty(Project.Test_Item.Run.Channel(i).Trans_Loc)
            Project.Test_Item.Run.Channel(i).Trans_Loc = 'Location';
        end
		if Project.Test_Item.Run.Channel(i).Orientation == '-'
            respdir = -1 * respdir;
		end
		temp = size(Project.Test_Item.Run.Channel(i).Trans_Loc,2);
		respnam = [Project.Test_Item.Run.Channel(i).Coordinate ...
                   Project.Test_Item.Run.Channel(i).Direction ...
                   Project.Test_Item.Run.Channel(i).Orientation];
		
        %  Start with header that's been updated for particular analysis
        header = head;
        %  Updata header fields for this channel    
		header = univhead('respdir',respdir,header);
		header = univhead('respnode',str2num(Project.Test_Item.Run.Channel(i).Coordinate),header);
		header = univhead('respnam',respnam,header);
		header = univhead('rec5',Project.Test_Item.Run.Channel(i).Trans_Loc,header);
	
        %  Check file status flag     
        if i == fchan+1 && status == 1
            status = 2;
%         else
%             status = 2;
        end
        
        %  Actually write the information to a file
		
	% 	uniwrite(Project.Test_Item.Run.Channel(i).Analysis(anal_num).Ord, header, filename, status);
		uniwrite_bpr(Project.Test_Item.Run.Channel(i).Analysis(anal_num).Ord,...
                     Project.Test_Item.Run.Channel(i).Analysis(anal_num).Abs,...
                     header, absspc, filename, status);
        nrec = nrec + 1;
	end
	message = sprintf('%d records of data written',nrec);
	disp(message)
else
    analyses_missing_error;
end
